package jp.co.apl.haven_enquete.android.activity;

import java.util.List;
import java.util.Map;

import jp.co.apl.haven.generated.HavenProtos.BasicInfo;
import jp.co.apl.haven.generated.HavenProtos.BasicInfoItem;
import jp.co.apl.haven.generated.HavenProtos.BasicInfoSelectionItem;
import jp.co.apl.haven.generated.HavenProtos.InputType;
import jp.co.apl.haven_enquete.android.R;
import jp.co.apl.haven_enquete.android.dao.BasicInfoDataDao;
import jp.co.apl.haven_enquete.android.dao.QuestionDataDao;
import jp.dev7.android.util.Activities;
import jp.dev7.android.util.Layouts;
import jp.dev7.android.util.Views;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TableLayout;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;

public class BasicInfoInput extends Activity {

    private Map<Integer, String> inputValues = Maps.newHashMap();

    private List<EditText> texts = Lists.newArrayList();

    private QuestionDataDao qDao;

    private BasicInfoDataDao bDao;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        qDao = new QuestionDataDao(this);
        bDao = new BasicInfoDataDao(this);

        // TODO あとで消す
        qDao.initDataFile();

        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
        setContentView(R.layout.basic_info);
        activityCommon.setActionBar("基本情報入力");
        Activities.findViewById(this, R.id.actionBar_nextButton).setVisibility(
            View.INVISIBLE);

        // ボタンへのイベントハンドラ付加
        Activities.setClickListnerToButton(
            this,
            R.id.basicInfo_okButton,
            new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    save();
                    cancel();
                }
            });
        Activities.setClickListnerToButton(
            this,
            R.id.basicInfo_cancelButton,
            new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    cancel();
                }
            });

        // 基本情報項目データをロード
        TableLayout table =
            Activities.findViewById(this, R.id.basicInfo_inputTable);
        // 基本情報データをロード
        Map<Integer, BasicInfo> data = bDao.loadAsMap();

        // 項目を画面には位置
        for (final BasicInfoItem item : qDao.load().getBasicInfoItemList()) {
            ViewGroup input = null;

            // 前回値
            String lastValue = null;
            if (data.get(item.getBasicInfoItemId()) != null) {
                lastValue = data.get(item.getBasicInfoItemId()).getAnswer();
            }

            if (item.getInputType() == InputType.TEXT) {
                // テキスト入力の場合

                input = Layouts.inflate(this, R.layout.basic_info_input_text);
                EditText text = Views.getChildAt(input, 1);
                text.setId(item.getBasicInfoItemId());
                // 前回値をセット
                if (lastValue != null) {
                    text.setText(lastValue);
                }
                texts.add(text);

            } else if (item.getInputType() == InputType.SELECT) {
                // 選択項目の場合

                input = Layouts.inflate(this, R.layout.basic_info_input_radio);

                RadioGroup radioGroup = Views.getChildAt(input, 1);
                radioGroup.setId(item.getBasicInfoItemId());

                int i = 0;
                for (final BasicInfoSelectionItem sel : item
                    .getBasicInfoSelectionItemList()) {
                    RadioButton radio = new RadioButton(this);
                    radio.setId(item.getBasicInfoItemId() * 1000 + i);
                    radio.setText(sel.getLabel());
                    // 前回値をセット
                    if (lastValue != null && sel.getValue().equals(lastValue)) {
                        radio.setChecked(true);
                    }
                    radioGroup.addView(radio, Layouts.getParamFillWrap());
                    i++;
                }

                // ラジオボタンが押された時の処理
                radioGroup
                    .setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
                        @Override
                        public void onCheckedChanged(RadioGroup group,
                                int checkedId) {
                            int index =
                                checkedId - item.getBasicInfoItemId() * 1000;
                            inputValues.put(item.getBasicInfoItemId(), item
                                .getBasicInfoSelectionItem(index)
                                .getValue());
                        }
                    });

            } else {
                continue;
            }

            // 項目名
            Views.setTextToChild(input, 0, item.getName());
            table.addView(input);
        }
    }

    @Override
    protected void onDestroy() {
        activityCommon = null;
        inputValues = null;
        texts = null;
        super.onDestroy();
    }

    protected void save() {
        for (EditText text : texts) {
            inputValues.put(text.getId(), text.getText().toString());
        }
        bDao.save(inputValues);
        Log.i("haven", "save basicInfo " + inputValues);
    }

    protected void cancel() {
        onBackPressed();
        finish();
    }

    private ActivityCommon activityCommon = new ActivityCommon(this);

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        super.onCreateOptionsMenu(menu);
        return activityCommon.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        super.onOptionsItemSelected(item);
        return activityCommon.onOptionsItemSelected(item);
    }
}
